<template>
  <div class="app-container">
    <!-- 头部 -->
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findData" />
    <el-card shadow="never" :body-style="{ padding: '5px' }" class="mt10">
      <div class="flex">
        <el-card shadow="never" :body-style="{ padding: '0px 10px' }">
          <div slot="header" class="f16">
            <span style="font-weight: 600">{{ $t(`Print.MaterialInventoryInquiry`) }}</span>
          </div>
          <Table
            :tableHeader="tableHeader"
            :tableData="tableData"
            class="mt10"
            @currentRow="currentRow"
            :outTableHeight="70"
            :isLoading="isLoading"
          />
        </el-card>
        <el-card shadow="never" :body-style="{ padding: '0px 5px' }">
          <div slot="header" class="f16">
            <span style="font-weight: 600">{{ $t(`Print.InstructionDetails`) }}</span>
          </div>
          <Table :tableHeader="tableHeader2" :tableData="tableData2" class="mt10" :outTableHeight="70" :isLoading="isLoading2" />
        </el-card>
        <el-card shadow="never" :body-style="{ padding: '0px' }">
          <el-card shadow="never" :body-style="{ padding: '0px 10px' }">
            <div slot="header" class="f16">
              <span style="font-weight: 600">{{ $t(`Print.InquireAboutTheWarehousingDetails`) }}</span>
            </div>
            <Table :tableHeader="tableHeader3" :tableData="tableData3" class="mt10" :TableHeight="50" :outTableHeight="100" :isLoading="isLoading2" />
          </el-card>
          <el-card shadow="never" :body-style="{ padding: '0px 10px' }">
            <div slot="header" class="f16">
              <span style="font-weight: 600">{{ $t(`Print.InquireAboutTheOutboundDetails`) }}</span>
            </div>
            <Table :tableHeader="tableHeader4" :tableData="tableData4" class="mt10" :TableHeight="50" :outTableHeight="100" :isLoading="isLoading2" />
          </el-card>
        </el-card>
      </div>
      <PageModule :PageData="{ total }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
    </el-card>
  </div>
</template>
<script lang="ts">
import { defineComponent, onMounted, ref, watch } from 'vue'
import JMessage from '@/utils/JMessage'
import fileRequest from '@/utils/fileRequest'
export default defineComponent({
  name: 'MaterialInventoryQuery'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import Find from '@/components/Find/index.vue'
import { selectList } from '@/plugins/mixin'
import service from '@/utils/request'
import { ElMessage } from 'element-plus'
import { debounce } from 'lodash'
// 表一模块
const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入物料编码',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入材料名称',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入材料颜色',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入指令',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入型体',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '分类',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'date',
    dateLabel: '入库日期',
    dateValue: ''
  },
  {
    type: 'date',
    dateLabel: '发料日期',
    dateValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入库位',
    inputValue: ''
  },
  {
    type: 'checkbox',
    checkboxLabel: '大于0',
    checked: true
  },
  {
    type: 'checkbox',
    checkboxLabel: '等于0',
    checked: true
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '导出'
  }
])
// '置空'按钮
const findData = async (data: any) => {
  if (data === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (data === '导出') {
    const res = (await fileRequest.put(`/pcFactorQuery/mtlAcc/export`, {
      cs: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '分类'),
      lotNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令'
      )?.inputValue, //	指令	string	body	true
      mtlColor: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入材料颜色'
      )?.inputValue, //	材料颜色	string	body	true
      mtlName: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入材料名称'
      )?.inputValue, //	材料名字	string	body	true
      mtlNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入物料编码'
      )?.inputValue, //	材料ID	string	body	true
      pageNum: currentPage.value, //		integer	body	true
      pageSize: pageSize.value, //		integer	body	true
      recEndDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '入库日期'
      )?.dateValue?.[1], //	入库时间末	string	body	true
      recStartDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '入库日期'
      )?.dateValue?.[0], //	入库时间始	string	body	true
      stkPosition: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入库位'
      )?.inputValue, //	库位	string	body	true
      stkQty: cfmFlag(), //	库存（大于零输入1，等于零输入0）	integer	body	true
      style: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入型体'
      )?.inputValue, //	型体	string	body	true
      traEndDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '发料日期'
      )?.dateValue?.[1], //	出库时间末	string	body	true
      traStartDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '发料日期'
      )?.dateValue?.[0] //	出库时间始	string	body	true
    })) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`utf-8''`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
}
// 表格数据
const tableData = ref([])
const tableHeader = ref([
  {
    label: '材料颜色',
    prop: 'mtlColor',
    width: '70px'
  },
  {
    label: '材料名称',
    prop: 'mtlName',
    width: '160px'
  },
  {
    label: '规格',
    prop: 'mtlSpec',
    width: 50
  },
  {
    label: '单位',
    prop: 'mtlUnit',
    width: 50
  },
  {
    label: '入库日期',
    prop: 'recDate',
    width: '85px'
  },
  {
    label: '发料日期',
    prop: 'traDate',
    width: '85px'
  },
  {
    label: '入库数量',
    prop: 'inQty',
    width: '70px'
  },
  {
    label: '发料数量',
    prop: 'outQty',
    width: '70px'
  },
  {
    label: '库存数量',
    prop: 'stkQty',
    width: '70px'
  },
  {
    label: '库位',
    prop: 'stkPosition',
    width: '80px'
  },
  {
    label: '物料编码',
    prop: 'mtlNo',
    width: '115px'
  }
])
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
// 表格计算函数
const cfmFlag = () => {
  const Unaudited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '大于0')
  const Audited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '等于0')
  if (!Unaudited || !Audited) return
  if (Unaudited.checked && Audited.checked) {
    return ''
  } else if (Unaudited.checked) {
    return '1'
  } else if (Audited.checked) {
    return '0'
  } else {
    ElMessage('必须至少选择一项')
    Unaudited.checked = true
    Audited.checked = true
    return ''
  }
}
const isLoading = ref(true)
// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.put<any, any>('/pcFactorQuery/queryMtlAccHead', {
    cs: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '分类'),
    lotNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令'
    )?.inputValue, //	指令	string	body	true
    mtlColor: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入材料颜色'
    )?.inputValue, //	材料颜色	string	body	true
    mtlName: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入材料名称'
    )?.inputValue, //	材料名字	string	body	true
    mtlNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入物料编码'
    )?.inputValue, //	材料ID	string	body	true
    pageNum: currentPage.value, //		integer	body	true
    pageSize: pageSize.value, //		integer	body	true
    recEndDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '入库日期'
    )?.dateValue?.[1], //	入库时间末	string	body	true
    recStartDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '入库日期'
    )?.dateValue?.[0], //	入库时间始	string	body	true
    stkPosition: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入库位'
    )?.inputValue, //	库位	string	body	true
    stkQty: cfmFlag(), //	库存（大于零输入1，等于零输入0）	integer	body	true
    style: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入型体'
    )?.inputValue, //	型体	string	body	true
    traEndDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '发料日期'
    )?.dateValue?.[1], //	出库时间末	string	body	true
    traStartDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '发料日期'
    )?.dateValue?.[0] //	出库时间始	string	body	true
  })
  tableData.value = tableDataList.dtos
  total.value = tableDataList.total
  isLoading.value = false
  currentRow()
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
const getSelectList = async () => {
  await selectList(formList, () => service.get('/pcFactorWork/getDept2Type'))
  const type = formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '分类') as any
  const res = await service.get<any, any>('/back/getMtlType3')
  res.forEach((Vendor: any) => {
    type.selectOptions.push({
      optionLabel: Vendor.typeName,
      optionValue: `${Vendor.typeNo}  ${Vendor.typeName}`,
      LeftLabel: `${Vendor.typeNo}`,
      RightLabel: `${Vendor.typeName}`
    })
  })
}
onMounted(() => {
  getSelectList()
})

// 表二模块
const tableHeader2 = ref([
  {
    label: '指令',
    prop: 'lotNo',
    width: '110px'
  },
  {
    label: '型体',
    prop: 'style',
    width: '100px'
  },
  {
    label: '入库量',
    prop: 'inQty',
    width: '70px'
  },
  {
    label: '出库量',
    prop: 'outQty',
    width: '70px'
  }
])
const tableData2 = ref([])
const tableData3 = ref([])
const tableData4 = ref([])
const isLoading2 = ref(true)
// 点击表给另一张表传数据
const currentRow = async (row: any = tableData.value[0] ? tableData.value[0] : {}) => {
  isLoading2.value = true
  let { mtlNo }: any = row
  // 给表二传指令号
  let table2Data = await service.get<any, any>(`/pcFactorQuery/queryMtlDetail/${mtlNo}`)
  const lotDetailData = table2Data.lotDetail.filter((item: any) => {
    if (item !== null) return true
  })
  const inDetailData = table2Data.inDetail.filter((item: any) => {
    if (item !== null) return true
  })
  const outDetailData = table2Data.outDetail.filter((item: any) => {
    if (item !== null) return true
  })
  tableData2.value = lotDetailData
  tableData3.value = inDetailData
  tableData4.value = outDetailData
  isLoading2.value = false
}

const tableHeader3 = ref([
  {
    label: '日期',
    prop: 'slipDate'
  },
  {
    label: '单据号码',
    prop: 'slipNo',
    width: '105px'
  },
  {
    label: '单据类型',
    prop: 'slipType'
  },
  {
    label: '数量',
    prop: 'qty'
  }
])
const tableHeader4 = ref([
  {
    label: '日期',
    prop: 'slipDate'
  },
  {
    label: '单据号码',
    prop: 'slipNo',
    width: '105px'
  },
  {
    label: '单据类型',
    prop: 'slipType'
  },
  {
    label: '数量',
    prop: 'qty'
  }
])
</script>

<style lang="less" scoped></style>
